package com.muyu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.system.domain.SysRoleMenu;
import com.muyu.system.mapper.SysRoleMenuMapper;
import com.muyu.system.service.SysRoleMenuService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色与菜单关联业务实现层
 * @author dongzeliang
 * @version 1.0
 * @description: 角色与菜单关联业务实现层
 * @date 2025/2/13 19:11
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper,SysRoleMenu> implements SysRoleMenuService {
    /**
     * 查询菜单下是否含有角色
     *
     * @param menuId 菜单ID
     * @return 含有角色数量
     */
    @Override
    public boolean checkMenuExistRole(Long menuId) {
        LambdaQueryWrapper<SysRoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenu::getMenuId, menuId);
        return count(lambdaQueryWrapper) > 0;
    }

    /**
     * 根据角色ID删除与菜单关联关系
     *
     * @param roleId 角色ID
     */
    @Override
    public void deleteRoleMenuByRoleId(Long roleId) {
        LambdaQueryWrapper<SysRoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenu::getRoleId, roleId);
        this.remove(lambdaQueryWrapper);
    }

    /**
     * 通过角色ID集合删除与菜单关联关系
     *
     * @param roleIdList 角色ID集合
     */
    @Override
    public void deleteRoleMenu(List<Long> roleIdList) {
        LambdaQueryWrapper<SysRoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SysRoleMenu::getRoleId, roleIdList);
        this.remove(lambdaQueryWrapper);
    }
}
